Skip to content

Support baking references.#940

Merged
markstory merged 3 commits into4.xfrom
4.x-references
Nov 8, 2025
Merged

Support baking references.#940
markstory merged 3 commits into4.xfrom
4.x-references

Conversation

@dereuromark
Copy link
Copy Markdown
Member

@dereuromark dereuromark commented Nov 3, 2025

Resolves #318

I put it into 4.x as it should be pretty safe, given it adds only specific parsing on top.
So this can already be used before we go to the next major. Especially since we are going Cake5.3+.
This would probably be the last minor release on that 4.x branch then.

Let me know if we should target 5.x right away, though.

@dereuromark dereuromark added this to the 4.x (CakePHP 5) milestone Nov 3, 2025
@dereuromark dereuromark marked this pull request as ready for review November 3, 2025 03:53
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for creating foreign key constraints using the references type in the migration bake command. The feature allows developers to specify foreign key relationships directly in the command line when generating migrations, automatically handling the column creation, foreign key constraint setup, and table name inference.

Key changes:

  • New parseForeignKeys() method to extract and process foreign key definitions from command arguments
  • Template updates to render foreign key constraints in generated migration files
  • Enhanced argument parsing to handle the references type and convert it to integer columns with foreign key constraints

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/Util/ColumnParser.php Adds foreign key parsing logic with automatic table name inference from field names
src/Command/BakeMigrationCommand.php Integrates foreign key parsing and updates help documentation with examples
templates/bake/config/skeleton.twig Adds template logic to render foreign key constraints in generated migrations
tests/TestCase/Util/ColumnParserTest.php Adds comprehensive test coverage for foreign key parsing functionality
tests/TestCase/Command/BakeMigrationCommandTest.php Adds integration tests and teardown cleanup for foreign key migration tests
tests/comparisons/Migration/*.php Expected output files for migration generation tests

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

* The <warning>index</warning> attribute can define the column as having a unique
key with <warning>unique</warning> or a primary key with <warning>primary</warning>.
* Use <warning>references</warning> type to create a foreign key constraint.
e.x. <warning>category_id:references</warning> (auto-infers table as 'categories')
Copy link

Copilot AI Nov 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected abbreviation from 'e.x.' to 'e.g.' (exempli gratia - for example).

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot open a new pull request to apply changes based on this feedback

Comment on lines +60 to +79
$files = glob(ROOT . DS . 'config' . DS . 'Migrations' . DS . '*_*Posts.php');
if ($files) {
foreach ($files as $file) {
unlink($file);
}
}

$files = glob(ROOT . DS . 'config' . DS . 'Migrations' . DS . '*_*Articles.php');
if ($files) {
foreach ($files as $file) {
unlink($file);
}
}

$files = glob(ROOT . DS . 'config' . DS . 'Migrations' . DS . '*_*Products.php');
if ($files) {
foreach ($files as $file) {
unlink($file);
}
}
Copy link

Copilot AI Nov 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The teardown cleanup logic is duplicated for three different file patterns. Consider extracting this into a helper method that accepts a pattern parameter to reduce code duplication.

Copilot uses AI. Check for mistakes.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Copy Markdown

Copilot AI commented Nov 4, 2025

@dereuromark I've opened a new pull request, #946, to work on those changes. Once the pull request is ready, I'll request review from you.

* Initial plan

* Fix abbreviation: change 'e.x.' to 'e.g.' in help text

Co-authored-by: dereuromark <39854+dereuromark@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: dereuromark <39854+dereuromark@users.noreply.github.com>
@markstory markstory merged commit c705be2 into 4.x Nov 8, 2025
13 checks passed
@markstory markstory deleted the 4.x-references branch November 8, 2025 05:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cake Migrations support References of tables Fathers and Childs

4 participants